package com.shj.web.controller;

import com.shj.model.PageRequest;
import com.shj.model.PageResult;
import com.shj.model.SysUser;
import com.shj.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;


/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2020-03-12 12:49:36
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;


    /**
     * @description 获取数据并分页
     * @author songhuajian
     * @date 2020/3/19 17:15
     */
    @GetMapping("/list")
    @ResponseBody
    public PageResult selectPage(PageRequest pageRequest){
        return sysUserService.findPage(pageRequest);
    }
    /**
     * 根据id获取用户数据
     */
    @PostMapping("/one")
    @ResponseBody
    public SysUser findOne(@RequestBody HashMap<String, String> map) {
        String userId = map.get("userId");
        return sysUserService.queryByUserId(userId);
    }

    /**
     * @description 添加用户
     * @author songhuajian
     * @date 2020/3/24 13:06
     */
    @PostMapping("/add")
    @ResponseBody
    public void addUser(@RequestBody @Valid SysUser sysUser) throws Exception {
        sysUserService.insert(sysUser);
    }

    /**
     * @description 更新用户
     * @author songhuajian
     * @date 2020/3/24 13:06
     */
    @PostMapping("/edit")
    @ResponseBody
    public void updateUser(@RequestBody @Valid SysUser sysUser) throws Exception {
        sysUserService.update(sysUser);
    }

    /**
     * @description 删除用户
     * @author songhuajian
     * @date 2020/3/24 13:06
     */
    @PostMapping("/delete")
    @ResponseBody
    public void delete(@RequestBody HashMap<String, String> map) throws Exception {
        String id = map.get("id");
        sysUserService.deleteById(Integer.valueOf(id));
    }
}